package net.hailxenu.stats;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class XenuStats extends JavaPlugin
{
    public Logger logger = Logger.getLogger("Minecraft");
    public String pluginName = "XenuStats";
    public String pluginVersion = "1.0.0.1";
    private StatsPlayer playerListener;
    private StatsBlocks blockListener;
    private StatsEntity entityListener;
    public Updater updater;
    private Thread updaterThread;
    public Socket sock;
    public HashMap<String, PlayerData> playerData;

    public void onEnable()
    {
        playerListener = new StatsPlayer(this);
        blockListener = new StatsBlocks(this);
        entityListener = new StatsEntity(this);
        updater = new Updater(this);
        playerData = new HashMap<String, PlayerData>();

        if(getServer().getOnlinePlayers().length > 0)
        {
            for(Player p : getServer().getOnlinePlayers())
            {
                playerData.put(p.getName(), new PlayerData(p.getName()));
            }
        }

        updaterThread = new Thread(updater);
        updaterThread.start();

        PluginManager pm = getServer().getPluginManager();
        PluginDescriptionFile pdfFile = this.getDescription();

        // STANDARD
        pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.BLOCK_PLACED, blockListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_ANIMATION, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Normal, this);
        pm.registerEvent(Event.Type.ENTITY_DEATH, entityListener, Priority.Normal, this);

        logger.log(Level.INFO, pluginName + " Version " + pluginVersion + " loaded.");
    }

    public void onDisable()
    {
        try
        {
            updaterThread.stop();

            sock.shutdownInput();
            sock.shutdownOutput();
            sock.close();
        } catch (IOException ex)
        {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    public PlayerData getPlayerData(Player player)
    {
        if(!playerData.containsKey(player.getName()))
        {
            playerData.put(player.getName(), new PlayerData(player.getName()));
        }

        return playerData.get(player.getName());
    }
}
